<?php
/**
 * 登录退出类
 * 控制用户的登录和退出
 * 
 * @author Lee <a605333742@gmail.com>
 * @time 2016-05-13
 */
namespace Admin\Controller;

class ManagerController extends EmptyController{
    /*
     * 构造函数
     * 
     * @return  #
     */
    public function _initialize(){
        $this->db   =new \Model\UserModel();
    }
    
    /*
     * 验证码验证
     * 
     * @return #
     */
    function verifyImg(){
        //以下类Verify在之前并没有include引入
        //走自动加载Think.class.php  autoload()
        $config =array(
            'imageH'    =>24,       // 验证码图片高度
            'imageW'    =>105,      //宽度
            'fontSize'  =>11,       //高度
            'useCurve'  =>false,    //曲线干扰
            'fontttf'   =>'5.ttf',  // 验证码字体，不设置随机获取
            'length'    =>4,        // 验证码位数
        );
        $verify =new \Think\Verify($config);
        $verify -> entry();        
    }
    
    /*
     * 登陆页面
     * 
     * @return #
     */
    public function login(){
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->display();
        } else {
            $this->redirect('Index/index');
        }
    }
    
    /*
     * 验证登录信息
     * 
     * @return #
     */
    public function checkLogin(){
        if(empty($_POST)){
            $this->redirect("login");
        }
        $map['tel|nickName']=I("post.user_name","","trim");
        $map['type']        =array('gt',0);
        $authInfo           =D("User")->where($map)->find();        
        if(!$authInfo){
            $this->error("账号不存在或已禁用",U(C('USER_AUTH_GATEWAY')));
        }else{
            if($authInfo['password'] !=I('post.password',"",'md5')){
                    $this->error("用户名或密码错误",U(C('USER_AUTH_GATEWAY')));
            }else{
                //保存用户认证标记名称
                $_SESSION[C('USER_AUTH_KEY')]   =$authInfo['id'];
                $_SESSION['nickName']		=$authInfo['nickname'];
                $_SESSION['add_time']           =$authInfo['add_time'];
                $_SESSION['companyName']        =$authInfo['companyname'];
                $_SESSION['last_logintime']     =$authInfo['last_logintime'];
                $_SESSION['client_ip']          =$authInfo['client_ip'];
                $_SESSION['type']               =$authInfo['type'];
                $_SESSION['integral']           =$authInfo['integral'];
                $_SESSION['icon']               =$authInfo['icon'];
                $_SESSION['state']              =$authInfo['state'];
                $_SESSION["tel"]                =$authInfo['tel'];
                $_SESSION["addroduction"]       =$authInfo['addroduction'];
                $_SESSION["websit"]             =$authInfo['websit'];
                //更新登录时间，登陆次数
                $user=M("User");
                $data['id']             =$authInfo['id'];
                $data['last_logintime'] =time();
                $data['ip']             =get_client_ip();
                $user->save($data);
                $this->redirect('Index/index'); 
            }                
        } 
    }
    
    /**
     * 修改自己的密码
     * 
     * @return #
     */
    public function changePwd(){
        if(!empty($_POST)){
            $where=array(
                'id'            =>session('authId'),
                'oldPassword'   =>I("post.oldPassword",'',"md5"),
                'password'      =>I("post.newPassword",'',"md5")
            );
            $result =new \Model\UserModel();
            $res    =$result->changePwd($where);
            if($res){
                $this->success("密码修改成功");
            }else{
                $this->error('密码修改失败！');
            }
        }else{
            $this->display();
        }
    }  
    
    /*
     * 退出系统
     * 
     * @return #
     */
    public function logout(){
       if (isset($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            session('[destroy]');
            $this->redirect(C("USER_AUTH_GATEWAY"));
        } else {
            $this->error('已经登出！');
        }
    }  
    
    
    /*
     * 同步验证验证码
     * 
     * @return bool
     */
    function ajaxCode(){
        $verify=new \Think\Verify();
        if(!$verify ->check(I("post.captcha","","trim"))){
            echo 1;
        }
    }
    
    /*
     * 注册显示页面
     * 
     * @return  #
     */
    public function register(){
        $this->assign("sign",1);
        $this->display("login");
    }
    
    /*
     * 注册
     * 
     * @return #
     */
    public function reg(){
        $map['type']        =1;
        $map['tel']         =I("request.tel");
        $map['password']    =I("request.password",'','md5');
        $map['repassword']  =I("request.repassword",'','md5');
        $map['nickName']    =I("request.user_name");
        $map['companyName'] =I("request.companyName");
        $map['state']       =1;
        if(!$this->db->create($map)){
            exit($this->db->getError());
        }else{
            if($this->db->reg($map)){
                $this->success("注册成功，请登录",U("login"));
            }else{
                $this->error("注册失败",U("register"));
            }
        }
    }
}

